﻿using AyaWo.Entity;
using Commen;
using DAL;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace WinService
{
    public class SplerDo
    {
        List<SpiderContent> lc = new List<SpiderContent>();
        public void Doto()
        {
            try
            {
                ///it
                string url = "http://tech.163.com/special/it_2016/";
                var docText = HtmlHelper.GetHtmlContent(url);
                //加载源代码，获取文档对象
                var doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(docText);

                var ListLi = doc.DocumentNode.SelectNodes("//ul[@class='newsList']/li");
                if (ListLi == null)
                    return;
                foreach (var t in ListLi)
                {
                    var q = t;
                    string href = string.Empty;
                    string title = string.Empty;
                    string souce = string.Empty;
                    string time = string.Empty;

                     href = q.SelectSingleNode("div/h3/a").Attributes["href"].Value;
                     title = q.SelectSingleNode("div/h3/a").InnerText;
                     souce = q.SelectSingleNode("//div[@class='newsBottom clearfix']//p[@class='sourceDate']/span").InnerText;
                     time = q.SelectSingleNode("//div[@class='newsBottom clearfix']//p[@class='sourceDate']").InnerText.Replace(souce, "");
                    if (CanInsert(title, href))
                    {
                        if (SpiderContentService.CanInsert(title, href))
                        {
                            var ty = new SpiderContent { Title = title, Link = href, Source = souce, BuildTime = time };
                            lc.Add(ty);
                        }
                    }
                    q = null;
                }

                foreach (var q in lc)
                {
                    if (SpiderContentService.CanInsert(q.Title, q.Link))
                    {
                        GetContent2(q);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("SplerDo : "+ex.Message);
            }
        }

        bool CanInsert(string title, string link)
        {
            var q = lc.Where(x => x.Link == link && x.Title == title).FirstOrDefault();
            if (q == null)
                return true;
            else
                return false;
        }

        public void GetContent2(SpiderContent q)
        {
            try
            {
                if (q == null || q.Link == null)
                    return;

                string url = q.Link;
                var doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(HtmlHelper.GetHtmlContent(url));
                var main_title = doc.DocumentNode.SelectSingleNode("//div[@id=\"epContentLeft\"]/h1").InnerText;  //标题
                var date_source = q.BuildTime;//发布时间
                var source = q.Source;

                string Gid = CommonHelper.CreateNo(); // Guid.NewGuid().ToString("N");

                ///todo   对文章内容的处理   1 下载图片到指定的位置   2.替换其中下载成功的图片的链接地址
                var body = doc.DocumentNode.SelectSingleNode("//div[@id=\"endText\"]");
                if (body == null)
                    return;
                if(body.SelectSingleNode("//div[@class='gg200x300']")!=null)
                body.SelectSingleNode("//div[@class='gg200x300']").InnerHtml = "";
                if(body.SelectSingleNode("//p[@class='otitle']")!=null)
                body.SelectSingleNode("//p[@class='otitle']").InnerHtml = "";
                if(body.SelectSingleNode("//span[@class='left']")!=null)
                q.Source = body.SelectSingleNode("//span[@class='left']").InnerText.Replace("本文来源：", "");
                if(body.SelectSingleNode("//span[@class='ep-editor']")!=null)
                q.Autos = body.SelectSingleNode("//span[@class='ep-editor']").InnerText.Replace("责任编辑：","");

                if(body.SelectSingleNode("//span[@class='left']")!=null)
                body.SelectSingleNode("//span[@class='left']").InnerHtml = "";
                if(body.SelectSingleNode("//span[@class='ep-editor']")!=null)
                body.SelectSingleNode("//span[@class='ep-editor']").InnerHtml = "";

                ///处理下载图片
                HtmlNodeCollection img = body.SelectNodes("//p[@class='f_center']");

                if (img != null && img.Count() > 0)
                {
                    foreach (HtmlNode child in img)
                    {
                        //    GetHtmlImageUrlList
                        var cc = child.SelectSingleNode("img");
                        if (cc == null)
                            continue;
                        string imgurl = cc.Attributes["src"].Value;
                        var newsrc = DownLoadImg(imgurl, Gid);
                        cc.SetAttributeValue("src", newsrc);
                    }
                }
    
                var tty = body;
                SpiderContentService.InsertContent(new SpiderContent
                    {
                        ID = Gid,
                        Title = main_title,
                        Link = q.Link,
                        Source = q.Source,
                        BuildTime = q.BuildTime,
                        Content = body.InnerHtml.Replace("网易科技讯", "阿雅沃网络科技报").Trim(),
                        CreateTime = DateTime.Now
                    });
            }
            catch (Exception ex)
            {
                Log.Error("GetContent2 : "+ex.Message);
            }
        }

        private string DownLoadImg(string imgurl, string Content_pathName)
        {
            var fileName = imgurl;
            var fileFolder = AppDomain.CurrentDomain.BaseDirectory + "\\Images\\" + Content_pathName + "\\";

            if (!Directory.Exists(fileFolder))
                Directory.CreateDirectory(fileFolder);
            try
            {
                WebClient wc = new WebClient();
                var path = System.IO.Path.GetFileName(fileName);
                path = path.Substring(0, path.IndexOf('?'));
                wc.DownloadFile(@"" + fileName, fileFolder + path);
                return "/Images/" + Content_pathName + "/" + path;
            }
            catch (Exception ex)
            {
                Log.Error("图片下载时的错误信息：" + ex.Message);
                return "";
            }
        }
    }
}
